#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2525;
int MX = 2521;

int n, m;
struct Node {
  int a, b, c;
} p[11];
int f[N][11];
int sum[N];
int solve() {
  rep(i, 1, n) f[1][i] = p[i].c;
  sum[1] = 0;
  rep(i, 1, n) sum[1] += f[1][i] > p[i].a ? 1 : 0;
  if (sum[1] == 0) return 1;
  rep(t, 2, MX) {
    rep(i, 1, n) {
      f[t][i] = f[t - 1][i] + 1;
      if (f[t][i] > p[i].a + p[i].b) f[t][i] = 1;
      if (f[t][i] == p[i].a + 1 && sum[t - 1] * 2 <= n) f[t][i] = 1;
    }
    sum[t] = 0;
    rep(i, 1, n) sum[t] += f[t][i] > p[i].a;
    if (sum[t] == 0) return t;
  }
  return -1;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // freopen("1.txt", "r", stdin);
  // freopen("2.txt", "w", stdout);
  int id = 0;
  while (cin >> n && n != 0) {
    ++id;
    rep(i, 1, n) {
      cin >> p[i].a >> p[i].b >> p[i].c;
      p[i].c = (p[i].c - 1) % (p[i].a + p[i].b) + 1;
    }
    int t = solve();
    cout << "Case " << id << ": " << t << endl;
  }
  return 0;
}